
//--------------------------------------------------------------------//
//     此代码由T4模板自动生成										  //
//	   生成时间 2020-03-25 14:20:46 	                              //
//	   描述：Base_AnnexesFile
//--------------------------------------------------------------------//
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System.Collections.Generic;
using System.IO;
using System.Data;
using RayD.App.SystemModule;
using RayD.Repository.Entites.SystemModule;
using RayD.Core;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Authorization;
using System.Linq;
using System;
using RayD.WebApi.Models;
using Microsoft.AspNetCore.Http;
using RayD.Repository;

namespace RayD.WebApi.Controllers.Admin
{
    public class AnnexesFileController : BaseAdminController
    {
        #region 构造函数注入
        private AnnexesFileApp _AnnexesFile;
        public AnnexesFileController(AnnexesFileApp AnnexesFile)
        {
            this._AnnexesFile = AnnexesFile;
        }
        #endregion

        #region 请求参数

        #endregion

        #region 获取数据
        /// <summary>
        /// 下载附件
        /// </summary>
        /// <param name="fileId">文件id</param>
        /// <returns></returns>
        [HttpGet, AllowAnonymous]
        public IActionResult DownAnnexesFile(string fileId)
        {
            var entity = _AnnexesFile.GetEntity(fileId);
            var fileInfo = IOHelper.Read(entity.B_FilePath);
            if (fileInfo.IsEmpty()) return NotFound();
            return File(fileInfo.FileStream, fileInfo.ContentType, entity.B_FileName);
        }
        /// <summary>
        /// 获取附件列表
        /// </summary>
        /// <param name="folderId">附件夹主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<AnnexesFileEntity>> GetAnnexesFileList(string folderId)
        {
            var list = _AnnexesFile.GetListByFolderId(folderId);
            return Success(list);
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 多附件上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResParameter<AnnexesFileEntity> Upload()
        {
            AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
            //保存到硬盘
            var fileInfo = IOHelper.Save(AppSettings.UploadPath.fileAnnexes);
            //没有文件上传，直接返回
            if (fileInfo.IsEmpty())
            {
                return Fail("没有文件上传!", fileAnnexesEntity);
            }
            var folderId = Request.Form["folderId"].ToString();

            //文件信息写入数据库
            fileAnnexesEntity.B_Id = fileInfo.FileGuid;
            fileAnnexesEntity.B_FolderId = folderId;
            fileAnnexesEntity.B_FileName = fileInfo.FileName;
            fileAnnexesEntity.B_FilePath = fileInfo.VirtualPath;
            fileAnnexesEntity.B_FileSize = fileInfo.FileSize;
            fileAnnexesEntity.B_FileExtensions = fileInfo.FileExtension;
            fileAnnexesEntity.B_FileType = fileInfo.FileExtension.Replace(".", "");
            var (isSuccess, msg) = _AnnexesFile.SaveForm(fileAnnexesEntity);
            if (isSuccess)
            {
                return Success(fileAnnexesEntity);
            }
            else
            {
                return Fail(msg, fileAnnexesEntity);
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="id">文件主键</param>
        /// <returns></returns>
        [HttpPut, Logging("附件删除")]
        public ResParameter DeleteAnnexesFile([FromQuery] string id)
        {
            AnnexesFileEntity fileInfoEntity = _AnnexesFile.GetEntity(id);
            _AnnexesFile.DeleteForm(fileInfoEntity);
            IOHelper.Delete(fileInfoEntity.B_FilePath);
            return Success("删除附件成功");
        }
        #endregion
    }
}
